package com.ruoyi.hxf.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hxf.domain.Education;
import com.ruoyi.hxf.service.IEducationService;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.service.ISysOssService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
/**
 * 摘要 信息操作处理
 *
 * @author hxf
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/information/education")
public class EducationController extends BaseController {

    private final IEducationService iEducationService;
    private final ISysOssService iSysOssService;

    /**
     * 获取教育摘要列表
     */
//    @SaCheckPermission("information:education:list")
    @GetMapping("/list")
    public TableDataInfo<Education> list(Education education, PageQuery pageQuery) {
        TableDataInfo<Education> educationTableDataInfo = iEducationService.selectPageEducationList(education, pageQuery);
//        for (Education row : educationTableDataInfo.getRows()) {
//            if(row.getEducationPicture()!=null){
//                String[] split = row.getEducationPicture().split(",");
//                StringBuffer stringBuffer = new StringBuffer();
//                for (String s : split) {
//                    List<SysOssVo> list = iSysOssService.listByIds(Arrays.asList(Long.parseLong(s)));
//                    for (int i = 0; i < list.size(); i++) {
//                        if (stringBuffer.length() == 0) {
//                            stringBuffer.append(list.get(i).getUrl());
//                        } else {
//                            stringBuffer.append("," + list.get(i).getUrl());
//                        }
//                    }
//                }
//                row.setEducationUrl(stringBuffer.toString());
//                iEducationService.myUpdateEducation(row);
//            }
//        }
        return educationTableDataInfo;
    }
    /**
     * 获取教育摘要列表
     */
    @GetMapping("/AppList")
    public TableDataInfo<Education> AppList(Education education, PageQuery pageQuery) {
        TableDataInfo<Education> educationTableDataInfo = iEducationService.selectPageEducationList(education, pageQuery);
        return educationTableDataInfo;
    }
    /**
     * 根据通知摘要编号获取详细信息
     *
     * @param educationId 公告ID
     */
//    @SaCheckPermission("information:education:query")
    @GetMapping(value = "/{educationId}")
    public R<Education> getInfo(@PathVariable Long educationId) {
        Education education = iEducationService.selectEducationById(educationId);
//        if(education.getEducationPicture()!=null){
//            String[] split = education.getEducationPicture().split(",");
//            StringBuffer stringBuffer = new StringBuffer();
//            for (String s : split) {
//                List<SysOssVo> list = iSysOssService.listByIds(Arrays.asList(Long.parseLong(s)));
//                for (int i = 0; i < list.size(); i++) {
//                    if (stringBuffer.length() == 0) {
//                        stringBuffer.append(list.get(i).getUrl());
//                    } else {
//                        stringBuffer.append("," + list.get(i).getUrl());
//                    }
//                }
//            }
//            education.setEducationUrl(stringBuffer.toString());
//            iEducationService.myUpdateEducation(education);
//        }
        return R.ok(education);
    }

    /**
     * 新增通知公告
     */
//    @SaCheckPermission("information:education:add")
    @Log(title = "教育摘要", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody Education education) {
        if(education.getEducationPicture()!=null){
            String[] split = education.getEducationPicture().split(",");
            StringBuffer stringBuffer = new StringBuffer();
            for (String s : split) {
                List<SysOssVo> list = iSysOssService.listByIds(Arrays.asList(Long.parseLong(s)));
                for (int i = 0; i < list.size(); i++) {
                    if (stringBuffer.length() == 0) {
                        stringBuffer.append(list.get(i).getUrl());
                    } else {
                        stringBuffer.append("," + list.get(i).getUrl());
                    }
                }
            }
            education.setEducationUrl(stringBuffer.toString());
        }
        return toAjax(iEducationService.insertEducation(education));
    }

    /**
     * 修改教育摘要
     */
//    @SaCheckPermission("information:education:edit")
    @Log(title = "教育摘要", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody Education education) {
        if(education.getEducationPicture()!=null){
            String[] split = education.getEducationPicture().split(",");
            StringBuffer stringBuffer = new StringBuffer();
            for (String s : split) {
                List<SysOssVo> list = iSysOssService.listByIds(Arrays.asList(Long.parseLong(s)));
                for (int i = 0; i < list.size(); i++) {
                    if (stringBuffer.length() == 0) {
                        stringBuffer.append(list.get(i).getUrl());
                    } else {
                        stringBuffer.append("," + list.get(i).getUrl());
                    }
                }
            }
            education.setEducationUrl(stringBuffer.toString());
        }else {
            education.setEducationUrl(null);
        }
        education.setUpdateBy(LoginHelper.getUsername());
        education.setUpdateTime(DateUtils.getNowDate());
        return toAjax(iEducationService.myUpdateEducation(education));
    }

    /**
     * 删除教育摘要
     *
     * @param educationIds 教育摘要ID串
     */
//    @SaCheckPermission("information:education:remove")
    @Log(title = "教育摘要", businessType = BusinessType.DELETE)
    @DeleteMapping("/{educationIds}")
    public R<Void> remove(@PathVariable Long[] educationIds) {
        return toAjax(iEducationService.deleteEducationByIds(educationIds));
    }

    /**
     * 获取三条教育摘要最新发布的消息
     * （手机端）
     *
     * @return
     */
    @GetMapping("/SelectCurrEducationLimit")
    public R<List<Education>> SelectCurrEducationLimit() {
        return R.ok(iEducationService.SelectCurrEducationLimit());
    }

    /**
     * 获取所有的教育摘要的信息
     * （手机端）
     *
     * @return
     */
    @GetMapping("/SelectCurrEducation")
    public R<List<Education>> SelectCurrEducation() {
        return R.ok(iEducationService.SelectCurrEducation());
    }
}
